{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learn to manage data collections using the generic list type\n",
    "\n",
    "*This tutorial teaches you C# interactively, using your browser to write C# code and see the results of compiling and running your code. It contains a series of lessons that create, modify, and explore collections and arrays.*\n",
    "\n",
    "## Create lists\n",
    "\n",
    "Run the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "var names = new List<string> { \"<name>\", \"Ana\", \"Felipe\" };\n",
    "foreach (var name in names)\n",
    "{\n",
    "    Console.WriteLine($\"Hello {name.ToUpper()}!\");\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You've created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier tutorials to loop through the list.\n",
    "\n",
    "The code to display names makes use of the *string interpolation* feature. When you precede a `string` with the `$` character, you can embed C# code in the string declaration. The actual string replaces that C# code with the value it generates. In this example, it replaces the `{name.ToUpper()}` with each name, converted to capital letters, because you called the `System.String.ToUpper` method.\n",
    "\n",
    "Let's keep exploring.\n",
    "\n",
    "## Modify list contents\n",
    "\n",
    "The collection you created uses the `System.Collections.Generic.List` type. This type stores sequences of elements. You specify the type of the elements between the angle brackets.\n",
    "\n",
    "One important aspect of this `System.Collections.Generic.List` type is that it can grow or shrink, enabling you to add or remove elements. You can see the results by modifying the contents after you've displayed its contents.Try the following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "Console.WriteLine();\n",
    "names.Add(\"Maria\");\n",
    "names.Add(\"Bill\");\n",
    "names.Remove(\"Ana\");\n",
    "foreach (var name in names)\n",
    "{\n",
    "    Console.WriteLine($\"Hello {name.ToUpper()}!\");\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You've added two more names to the end of the list. You've also removed one as well. The output from this block of code shows the initial contents, then prints a blank line and the new contents.\n",
    "\n",
    "The `System.Collections.Generic.List` enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Try it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Console.WriteLine($\"My name is {names[0]}.\");\n",
    "Console.WriteLine($\"I've added {names[2]} and {names[3]} to the list.\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You're not allowed to access past the end of the list. You can check how long the list is using the `System.Collections.Generic.List<T>.Count` property. Try it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Console.WriteLine($\"The list has {names.Count} people in it\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In C#, indices start at 0, so the largest valid index is one less than the number of items in the list.\n",
    "\n",
    "## Search and sort lists\n",
    "\n",
    "Our samples use relatively small lists, but your applications may often create lists with many more elements, sometimes numbering in the thousands. To find elements in these larger collections, you need to search the list for different items. The `System.Collections.Generic.List<T>.IndexOf` method searches for an item and returns the index of the item. Try it to see how it works:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "var index = names.IndexOf(\"Felipe\");\n",
    "if (index != -1)\n",
    "  Console.WriteLine($\"The name {names[index]} is at index {index}\");\n",
    "\n",
    "var notFound = names.IndexOf(\"Not Found\");\n",
    "  Console.WriteLine($\"When an item is not found, IndexOf returns {notFound}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may not know if an item is in the list, so you should always check the index returned by `System.Collections.Generic.List<T>.IndexOf`. If it is -1, the item was not found.\n",
    "\n",
    "The items in your list can be sorted as well. The `System.Collections.Generic.List<T>.Sort` method sorts all the items in the list in their normal order (alphabetically in the case of strings):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.Sort();\n",
    "foreach (var name in names)\n",
    "{\n",
    "  Console.WriteLine($\"Hello {name.ToUpper()}!\");\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lists of other types\n",
    "\n",
    "You've been using the `string` type in lists so far. Let's make a `List<T>` using a different type. Let's build a set of numbers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "var fibonacciNumbers = new List<int> {1, 1};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That creates a list of integers, and sets the first two integers to the value 1. The *Fibonacci Sequence*, a sequence of numbers, starts with two 1s. Each next Fibonacci number is found by taking the sum of the previous two numbers. Try this code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];\n",
    "var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];\n",
    "\n",
    "fibonacciNumbers.Add(previous + previous2);\n",
    "\n",
    "foreach(var item in fibonacciNumbers)\n",
    "    Console.WriteLine(item);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge\n",
    "\n",
    "See if you can put together some of the concepts from this and earlier lessons. Expand on what you've built so far with Fibonacci Numbers. Try and write the code to generate the first 20 numbers in the sequence. (As a hint, the 20th Fibonacci number is 6765.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
